//
//  TradePwdVC.swift
//  LeGou
//
//  Created by lxm on 2022/8/4.
//

import UIKit

class TradePwdVC: ZMViewController, UITableViewDelegate, UITableViewDataSource {
    //MARK: --------------------属性--------------------
    let padding : CGFloat = 15
    
    let data : [[(title : String, subTitle : String, icon : Bool, id : Int)]] = [
        [(title : "修改支付密码", subTitle : "", icon : true, id : 0),
         (title : "忘记支付密码", subTitle : "", icon : true, id : 1)]
      ]
    //MARK: --------------------UI控件--------------------
    let tableView = UITableView(frame: CGRect.zero, style: .plain).then { t in
        t.separatorStyle = .none
        t.estimatedSectionFooterHeight = 0.01
        t.estimatedSectionHeaderHeight = 0.01
        t.sectionFooterHeight = 0.01
        t.sectionHeaderHeight = 0.01
    }
    
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    
    //MARK: --------------------UI--------------------
    func setUI() {
        title = "系统设置"
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        
        let v = UIView()
        tableView.tableFooterView = v
        tableView.backgroundColor = "#F5F5F5".zm.hex()
        view.backgroundColor = "#F5F5F5".zm.hex()
        
        v.frame = CGRect(x: 0, y: 0, width: SCREEN_W, height: 120)
        v.backgroundColor = .clear
        
        tableView.zm.register(nibName: "SettingCell", reuserId: "SettingCell")
    }
    
    func addConstraints() {
        tableView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(padding)
            make.right.equalToSuperview().offset(-padding)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
     
    }

    //MARK: --------------------delegate--------------------
    func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[section].count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 16
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 48
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let v = UIView()
        v.backgroundColor = .red
        return v
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SettingCell") as! SettingCell
        let data = data[indexPath.section][indexPath.row]
        cell.titleLabel.text = data.title
        cell.subLabel.text = data.subTitle
        cell.rightIcon.isHidden = !data.icon
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let allCount = self.data[indexPath.section].count
        let current = indexPath.row
        if allCount == 1 {
            ZMUtil.clipCircle(view: cell, Corner: [.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 12)
        } else {
            if current == 0 {
                ZMUtil.clipCircle(view: cell, Corner: [.topLeft, .topRight,], radius: 12)
            } else if current == allCount - 1 {
                ZMUtil.clipCircle(view: cell, Corner: [.bottomLeft, .bottomRight], radius: 12)
            } else {
                cell.layer.mask = nil
            }
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = data[indexPath.section][indexPath.row]
        if item.id == 0 {
            let vc = TradePwdSet()
            vc.find = false
            vc.type = 0
            self.navigationController?.pushViewController(vc, animated: true)
        } else if item.id == 1 {
            let vc = TradeFroget()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}
